<?php 
/**
 * [M 实例化模型]
 * @param [type] $table [表名]
 */
function M($table){
	$obj = new Model($table);
	return $obj;
}
/**
 * [K description]
 * @param [type] $model [description]
 */
function K($model){
	return new $model."Model";
}

/**
 * [print_const 打印用户定义的常量]
 * @return [type] [description]
 */
function print_const(){
	$const = get_defined_constants();
	p($const['user']);
}

/**
 * [halt 打印错误信息]
 * @param  [type]  $error [错误内容]
 * @param  string  $Level [ERROR]
 * @param  integer $type  [保存为文件]
 * @param  [type]  $dest  [保存位置]
 * @return [type]         [description]
 */
function halt($error,$Level="ERROR",$type=3,$dest=NULL){
	if(is_array($error)){ //数组
		Log::writeLog($error['message'],$Level,$type,$dest);
	}else{ //字符串
		Log::writeLog($error,$Level,$type,$dest);
	}

	$err = array();
	if(DEBUG){
		if(!is_array($error)){
			$trace = debug_backtrace();
			$err['message'] = $error;
			$err['file'] = $trace[0]['file'];
			$err['line'] = $trace[0]['line'];
			$err['class'] = isset($trace[0]['class']) ? $trace[0]['class'] : "";
			$err['function'] = isset($trace[0]['function']) ? $trace[0]['function'] : "";
			ob_start();//开启缓冲区
			debug_print_backtrace();
			$err['trace'] = htmlspecialchars(ob_get_clean());
		}else{
			$err = $error;
		}
	}else{
		if($url = C('ERROR_URL')){
			go($url);
		}else{
			$err['message'] = C('ERROR_MSG');
		}
	}

	include SYSTEM_DATA_PATH . '/Tpl/halt.html';
	die;
}

/**
 * [p 打印函数]
 * @param  [type] $msg [需要打印的内容]
 * @return [type]      [description]
 */
function p($msg){
	if(is_bool($msg)){
		var_dump($msg);
	}else if (is_null($msg)) {
		var_dump(NULL);
	}else{
		echo "<pre style=\"padding:10px;border-radius:5px;background:#f5f5f5;border:1px solid #ccc;font-size:14px;\">".print_r($msg,true)."</pre>";
	}
}
/**
 * [go 跳转函数]
 * @param  [type]  $url  [跳转地址]
 * @param  [type]  $msg  [提示内容]
 * @param  integer $time [跳转时间]
 * @return [type]        [description]
 */
function go($url,$msg,$time = 0){
	if(!headers_sent()){
		$time == 0 ? header('Location:'.$url) : header("refresh:{$time};url={$url}");
		die($msg);
	}else{
		echo "<meta http-equiv='Refresh' content='{$time};url={$url}'/>";
		if($time) die($msg);
	}
}

/**
 * [C 读取和设置配置项]
 * @param [type] $var   [description]
 * @param [type] $value [description]
 */
function C($var = NULL,$value = NULL){
	//1加载配置项
	static $config = array();
	//2.读取配置项
	if(is_array($var)){
		//合并配置内容，设置用户优先级大于系统，设置key为大写
		$config = array_merge($config,array_change_key_case($var,CASE_UPPER));
		return;
	}

	if(is_string($var)){
		//转换大写
		$var = strtoupper($var);
		//3临时动态改变配置项
		if(!is_null($value)){
			$config[$var] = $value;
			return;
		}
		return isset($config[$var]) ? $config[$var] : NULL;
	}
	
	//4.c()读取所有配置项
	if(is_null($var) && is_null($value)){
		return $config;
	}
}
?>